#include "swmmodel.h"

SWMModel::SWMModel()
{

}

/*
 *  新建任务的流程
 */
bool SWMModel::createmodel(int id, int seq, int model, int time, QString type, QString dose, int temp)
{
    QSqlQuery query;
    if(!query.exec(QString("insert into SWM_Model(ModelID,Sequence,Model,Time,DetergentType,DetergentDose,Temperature) \
                            values('%1','%2','%3','%4','%5','%6','%7')")\
               .arg(id).arg(seq).arg(model).arg(time).arg(type).arg(dose).arg(temp)))
    {
        qDebug()<<query.lastError().text();
        return false;
    }
    return true;
}

/*
 *  新建任务的流程
 */
QStringList SWMModel::getmodelList(int id, int seq)
{
    QSqlQuery query;
    QStringList modelist;
    if(!query.exec(QString("select * from SWM_Model where ModelID='%1' and Sequence='%2'").arg(id).arg(seq)))
    {
        qWarning()<<query.lastError().text();
    }
    else {
        if(query.first())
        {
            QString Model = query.value("Model").toString();
            modelist.append(Model);
            QString Time = query.value("Time").toString();
            modelist.append(Time);
            QString DetergentType = query.value("DetergentType").toString();
            modelist.append(DetergentType);
            QString DetergentDose = query.value("DetergentDose").toString();
            modelist.append(DetergentDose);
            QString Temperature = query.value("Temperature").toString();
            modelist.append(Temperature);
        }
    }
    return modelist;
}

void SWMModel::createTaskFromModel(int id, int task_id)
{
    QSqlQuery query;
    QStringList modelist[4];
    int count = 0;
    if(!query.exec(QString("select * from SWM_Model where ModelID='%1'").arg(id)))
    {
        qWarning()<<query.lastError().text();
    }
    else {
        while(query.next())
        {
            QString Model = query.value("Model").toString();
            modelist[count].append(Model);
            QString Time = query.value("Time").toString();
            modelist[count].append(Time);
            QString DetergentType = query.value("DetergentType").toString();
            modelist[count].append(DetergentType);
            QString DetergentDose = query.value("DetergentDose").toString();
            modelist[count].append(DetergentDose);
            QString Temperature = query.value("Temperature").toString();
            modelist[count].append(Temperature);
            count++;
        }
    }
    for(int i=0; i<count; i++)
    {
        if(i==0)
        {
            if(!query.exec(QString("insert into SWM_Task(TaskID,Sequence,Status,Model,Time,DetergentType,DetergentDose,Temperature) \
                                    values('%1','%2', '正在执行','%3','%4','%5','%6','%7')")\
                       .arg(task_id).arg(i+1).arg(modelist[i].at(0)).arg(modelist[i].at(1))\
                        .arg(modelist[i].at(2)).arg(modelist[i].at(3)).arg(modelist[i].at(4))))
            {
                qDebug()<<query.lastError().text();
            }
        }
        else
        {
            if(!query.exec(QString("insert into SWM_Task(TaskID,Sequence,Status,Model,Time,DetergentType,DetergentDose,Temperature) \
                                   values('%1','%2', '未执行','%3','%4','%5','%6','%7')")\
                      .arg(task_id).arg(i+1).arg(modelist[i].at(0)).arg(modelist[i].at(1))\
                       .arg(modelist[i].at(2)).arg(modelist[i].at(3)).arg(modelist[i].at(4))))
            {
               qDebug()<<query.lastError().text();
            }
        }

    }
}

void SWMModel::deleteFromID(int id)
{
    QSqlQuery query;
   if(!query.exec(QString("delete from SWM_Model where ModelID='%1'").arg(id)))
   {
       qWarning()<<query.lastError().text();
   }
}
